/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RacaSelector.java
 *
 * Created on Apr 19, 2009, 10:08:18 AM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.RacaController;
import doghost.entities.Raca;

/**
 *
 * @author duo
 */
public class RacaSelector extends javax.swing.JDialog {

    protected RacaController racaController;
    protected Raca selectedRaca;

    /** Creates new form RacaSelector */
    public RacaSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        racaController = new RacaController();
        racaTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("racaRecordSelected",
                                !isRacaRecordSelected(), isRacaRecordSelected());
                    }
                });
    }

    public Raca getSelectedRaca() {
        return selectedRaca;
    }

    public void setSelectedRaca(Raca selectedRaca) {
        this.selectedRaca = selectedRaca;
    }

    public boolean isRacaRecordSelected() {
        return racaTable.getSelectedRow() != -1;
    }

    @Action
    public void newRaca() {
        racaController.newRaca(doghostPUEntityManager, racaList);
    }

    @Action(enabledProperty = "racaRecordSelected")
    public void editRaca() {
        int index = racaTable.getSelectedRow();
        racaController.setRaca(racaList.get(racaTable.
                convertRowIndexToModel(index)));
        racaController.editRaca(doghostPUEntityManager, racaList,
                racaTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "racaRecordSelected")
    public void deleteRaca() {
        int index = racaTable.getSelectedRow();
        racaController.setRaca(racaList.get(racaTable.
            convertRowIndexToModel(index)));
        racaController.deleteRaca(doghostPUEntityManager, racaList,
            racaTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelRacaSelector() {
        setSelectedRaca(null);
        setVisible(false);
    }

    @Action(enabledProperty = "racaRecordSelected")
    public void selectRaca() {
        int index = racaTable.getSelectedRow();
        setSelectedRaca(racaList.get(racaTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterRaca() {
        TableRowSorter racaSorter = (TableRowSorter) racaTable.getRowSorter();
        String text = racaFilterTextField.getText();
        racaFilterTextField.setText("");
        if(text.length()==0) {
            racaSorter.setRowFilter(null);
        } else {
            racaSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        racaQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery("SELECT r FROM Raca r");
        racaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(racaQuery.getResultList());
        racaFilterTextField = new javax.swing.JTextField();
        racaFilterButton = new javax.swing.JButton();
        racaSelectionPanel = new org.jdesktop.swingx.JXPanel();
        racaScrollPane = new javax.swing.JScrollPane();
        racaTable = new javax.swing.JTable();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        selectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(RacaSelector.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        racaFilterTextField.setName("racaFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(RacaSelector.class, this);
        racaFilterButton.setAction(actionMap.get("filterRaca")); // NOI18N
        racaFilterButton.setName("racaFilterButton"); // NOI18N

        racaSelectionPanel.setName("racaSelectionPanel"); // NOI18N

        racaScrollPane.setName("racaScrollPane"); // NOI18N

        racaTable.setAutoCreateRowSorter(true);
        racaTable.setName("racaTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, racaList, racaTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        racaScrollPane.setViewportView(racaTable);
        racaTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("racaTable.columnModel.title0")); // NOI18N

        javax.swing.GroupLayout racaSelectionPanelLayout = new javax.swing.GroupLayout(racaSelectionPanel);
        racaSelectionPanel.setLayout(racaSelectionPanelLayout);
        racaSelectionPanelLayout.setHorizontalGroup(
            racaSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(racaSelectionPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(racaScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 358, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        racaSelectionPanelLayout.setVerticalGroup(
            racaSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(racaScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
        );

        newButton.setAction(actionMap.get("newRaca")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editRaca")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteRaca")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelRacaSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        selectButton.setAction(actionMap.get("selectRaca")); // NOI18N
        selectButton.setName("selectButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 453, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(racaFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(racaFilterButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)
                        .addGap(44, 44, 44)
                        .addComponent(selectButton))
                    .addComponent(racaSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(racaFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(racaFilterButton))
                .addGap(18, 18, 18)
                .addComponent(racaSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(newButton)
                            .addComponent(editButton)
                            .addComponent(deleteButton)
                            .addComponent(cancelButton))
                        .addContainerGap(29, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectButton)
                        .addGap(29, 29, 29))))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RacaSelector dialog = new RacaSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton racaFilterButton;
    private javax.swing.JTextField racaFilterTextField;
    private java.util.List<doghost.entities.Raca> racaList;
    private javax.persistence.Query racaQuery;
    private javax.swing.JScrollPane racaScrollPane;
    private org.jdesktop.swingx.JXPanel racaSelectionPanel;
    private javax.swing.JTable racaTable;
    private javax.swing.JButton selectButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
